﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPatterns
{
    /// <summary>
    /// 人员迭代器
    /// 继承接口IEnumerator，显示实现了Current、MoveNext、Reset方法。
    /// </summary>
    abstract class PeopleIterator : IEnumerator
    {
        public abstract int Key();

        public abstract object Current();


        public abstract bool MoveNext();

        public abstract void Reset();

        object IEnumerator.Current
        {
            get { return Current(); }
        }
    }
    /// <summary>
    /// 抽象的集合迭代器
    /// </summary>
    public abstract class AggregateIterator : IEnumerable
    {
        /// <summary>
        /// 得到内部的集合
        /// </summary>
        /// <returns></returns>
        public abstract IEnumerator GetEnumerator();
    }

    /// <summary>
    /// 集合的迭代器类
    /// 具体排序的逻辑在这里写着
    /// </summary>
    class AlphabeticalOrderIterator : PeopleIterator
    {
        private PeopleCollection _collection;
        private int _position = -1;
        private bool _reverse = false;

        public AlphabeticalOrderIterator(PeopleCollection collection, bool reverse = false)
        {
            this._collection = collection;
            _reverse = reverse;

            if (reverse)
            {
                _position = collection.getItems().Count;
            }
        }

        /// <summary>
        /// 当前的值
        /// </summary>
        /// <returns></returns>
        public override object Current()
        {
            return this._collection.getItems()[_position];
        }

        /// <summary>
        /// 此时的索引
        /// </summary>
        /// <returns></returns>
        public override int Key()
        {
            return this._position;
        }

        /// <summary>
        /// 下一个  位置position进行调整
        /// </summary>
        /// <returns></returns>
        public override bool MoveNext()
        {
            int updatedPosition = this._position + (this._reverse ? -1 : 1);

            if (updatedPosition >= 0 && updatedPosition < this._collection.getItems().Count)
            {
                this._position = updatedPosition;
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 重置
        /// </summary>
        public override void Reset()
        {
            this._position = this._reverse ? this._collection.getItems().Count - 1 : 0;
        }
    }

    /// <summary>
    /// 具体的人员集合
    /// </summary>
    public class PeopleCollection : AggregateIterator
    {
        List<string> _collection = new List<string>();

        bool _direction = false;

        public void ReverseDirection()
        {
            _direction = !_direction;
        }

        public List<string> getItems()
        {
            return _collection;
        }

        public void AddItem(string item)
        {
            _collection.Add(item);
        }

        public override IEnumerator GetEnumerator()
        {
            return new AlphabeticalOrderIterator(this, _direction);
        }
    }

    class IteratorPattern
    {
    }
}
